﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Data;
using MyWeb.Common;
using System.Web.Configuration;

namespace MobileRecharge
{
    public partial class Recharge : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Username"] == null)
                {
                    Response.Redirect("Default.aspx");
                }
                GetServices();
            }
            
        }

        private void GetServices() {
            ddlService.Items.Clear();
            ListItem lstAll = new ListItem("Service", "-1");
            ddlService.Items.Add(lstAll);
            var dt = new DataTable();
            dt = tb_ServicesService.tb_Services_GetByAll();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem lstItem = new ListItem(dt.Rows[i]["Name"].ToString(),dt.Rows[i]["Id"].ToString());
                ddlService.Items.Add(lstItem);
            }
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                tb_PaymentInfo obj = new tb_PaymentInfo();
                obj.ServiceId = ddlService.SelectedValue;
                obj.DateTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond;
                obj.Amount = txtAmount.Text;
                obj.Phone = txtPhoneNumber.Text;
                obj.Status = "True";
                obj.UserId = Session["Username"].ToString();
                tb_PaymentService.tb_Payment_Insert(obj);
                Response.Redirect(paypal());
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
        public String paypal() {
            string action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
            string cmd = "?cmd=_xclick";
            string upload = "&upload=1";
            string business = "&business=" + System.Web.Configuration.WebConfigurationManager.AppSettings["paypalemail"];
            string item_name = "&item_name=Widget";
            string amount = "&amount=" + txtAmount.Text;
            string currency_code = "&currency_code=USD";
            string Return = "&return=" + System.Web.Configuration.WebConfigurationManager.AppSettings["SuccessURL"];
            string cancel_return = "&cancel_return=" + System.Web.Configuration.WebConfigurationManager.AppSettings["FailedURL"];
            string undefined_quantity = "&undefined_quantity";
            String url = action + cmd + upload + business + item_name + amount + currency_code + Return + cancel_return + undefined_quantity;
            
            return url;
        
        }

       
    }
}